package com.aysafety.ayelevator.listener;

import com.anyi.safe.communication.model.ElevatorAccident;
import com.anyi.safe.communication.model.ElevatorData;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.Common;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.ElevatorAlarmRealTimeModel;
import com.aysafety.core.model.ElevatorRealTimeModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ElevatorRealTimeListener {
    private final String clientId = "ay";
    private final String tableName = "ElevatorRealTime";

    private final String[] structure = new String[] {"RealTime", "AlarmData"};

    private Logger logger = LoggerFactory.getLogger(ElevatorRealTimeListener.class);

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public void start(ElevatorData elevatorData, ElevatorAccident elevatorAccident) {
        try {
            if (null != elevatorAccident) {
                ElevatorAlarmRealTimeModel elevatorAlarmRealTime =
                    this.transformElevatorAlarmRealTimeModel(elevatorAccident);
                this.saveAlarmData(elevatorAlarmRealTime, this.clientId);
            }
            ElevatorRealTimeModel elevatorRealTime = this.transformElevatorRealTimeModel(elevatorData);
            this.saveRealTime(elevatorRealTime, this.clientId);
        } catch (IOException e) {
            logger.info("ElevatorRealTimeListener:", e);
        }
    }

    public boolean saveRealTime(ElevatorRealTimeModel elevatorRealTime, String clientId) throws IOException {
        List<String> params = new ArrayList<String>();
        String columnFamily = "RealTime";
        String timestamp = String.valueOf(DateUtils.fomatDateTime(DateUtils.getTime()).getTime());
        params.add(elevatorRealTime.getDeviceId());
        params.add(clientId);
        String rowKey = Common.getRowKey(params, timestamp);
        if (!HBaseDriver.isExistRowByRowKey(this.tableName, rowKey)) {
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "clientId", clientId);
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "deviceId", elevatorRealTime.getDeviceId());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "direction", elevatorRealTime.getDirection());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "door", elevatorRealTime.getDoor());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "floor", elevatorRealTime.getFloor());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "flatState", elevatorRealTime.getFlatState());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "heightD", elevatorRealTime.getHeightD());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "humidityD", elevatorRealTime.getHumidityD());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "people", elevatorRealTime.getPeople());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "speed", elevatorRealTime.getSpeed());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "temperature", elevatorRealTime.getTemperature());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "realTime", Long.valueOf(timestamp));
        } else {
            logger.info("数据已存在，重复不更新");
        }
        return true;
    }

    public boolean saveAlarmData(ElevatorAlarmRealTimeModel elevatorAlarmRealTime, String clientId) throws IOException {
        List<String> params = new ArrayList<String>();
        String columnFamily = "AlarmData";
        String timestamp = String.valueOf(elevatorAlarmRealTime.getAlarmTime().getTime());
        params.add(elevatorAlarmRealTime.getDeviceId());
        params.add(clientId);
        String rowKey = Common.getRowKey(params, timestamp);
        if (!HBaseDriver.isExistRowByRowKey(this.tableName, rowKey)) {
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "clientId", clientId);
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "deviceId", elevatorAlarmRealTime.getDeviceId());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "alarmTime",
                elevatorAlarmRealTime.getAlarmTime().getTime());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "faultCode", elevatorAlarmRealTime.getFaultCode());
            HBaseDriver.addRow(this.tableName, rowKey, columnFamily, "faultDesc", elevatorAlarmRealTime.getFaultDesc());
        } else {
            logger.info("数据已存在，重复不更新");
        }
        return true;
    }

    public ElevatorAlarmRealTimeModel transformElevatorAlarmRealTimeModel(ElevatorAccident elevatorAccident) {
        ElevatorAlarmRealTimeModel elevatorAlarmRealTime = new ElevatorAlarmRealTimeModel();
        String elevCode = elevatorAccident.getElevCode();
        Date alarmTime = elevatorAccident.getAlarmTime();
        String faultCode = elevatorAccident.getFaultCode();
        String FaultDesc = elevatorAccident.getFaultDesc();
        elevatorAlarmRealTime.setDeviceId(elevCode);
        elevatorAlarmRealTime.setAlarmTime(alarmTime);
        elevatorAlarmRealTime.setFaultCode(faultCode);
        elevatorAlarmRealTime.setFaultDesc(FaultDesc);
        return elevatorAlarmRealTime;
    }

    public ElevatorRealTimeModel transformElevatorRealTimeModel(ElevatorData elevatorData) {
        ElevatorRealTimeModel elevatorRealTime = new ElevatorRealTimeModel();
        String deviceCode = elevatorData.getDeviceCode();
        Double speed = Double.parseDouble(elevatorData.getSpeed());
        Double heightD = Double.parseDouble(elevatorData.getHeight());
        Double temperature = Double.parseDouble(elevatorData.getTemperature());
        Double humidityD = Double.parseDouble(elevatorData.getHumidity());
        int floor = elevatorData.getFloor();
        int door = elevatorData.getDoor();
        int people = elevatorData.getPeople();
        String direction = elevatorData.getDirection();
        int flatState = 0;
        if (null != elevatorData.getFlatState()) {
            flatState = elevatorData.getFlatState();
        }
        elevatorRealTime.setDeviceId(deviceCode);
        elevatorRealTime.setSpeed(speed);
        elevatorRealTime.setHeightD(heightD);
        elevatorRealTime.setTemperature(temperature);
        elevatorRealTime.setHumidityD(humidityD);
        elevatorRealTime.setFloor(floor);
        elevatorRealTime.setDoor(door);
        elevatorRealTime.setPeople(people);
        elevatorRealTime.setDirection(direction);
        elevatorRealTime.setFlatState(flatState);
        return elevatorRealTime;
    }
}
